package libcommonuse11.compara.comparable;

import java.util.Arrays;

class Student implements Comparable<Student>
{
	private String name;
	private int score;
	private int age;
	public Student()
	{
		
	}
	public Student(String name, int score, int age)
	{
		super();
		this.setName(name);
		this.setScore(score);
		this.setAge(age);
	}

	public int compareTo(Student stu)
	{
		if(this.score > stu.score)
		{
			return -1;
		}
		else if(this.score < stu.score)
		{
			return 1;
		}
		else
		{
			if(this.age > stu.age)
			{
				return 1;
			}
			else if(this.age == stu.age)
			{
				return 0;
			}
			else
			{
				return -1;
			}
		}
		
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getName()
	{
		return name;
	}

	public void setScore(int score)
	{
		this.score = score;
	}

	public int getScore()
	{
		return score;
	}

	public void setAge(int age)
	{
		this.age = age;
	}

	public int getAge()
	{
		return age;
	}
	@Override
	public String toString()
	{
		return "Student [name=" + name + ", score=" + score + ", age=" + age + "]";
	}
	
}

public class ComparableDemo01
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		Student [] stu = new Student[5];
		stu[0] = new Student("小明",96,10);
		stu[1] = new Student("小王",99,11);
		stu[2] = new Student("小孙",55,9);
		stu[3] = new Student("小李",55,11);
		stu[4] = new Student("小猛",99,19);
		System.out.println("排序之前：");
		for(int i=0;i<stu.length;i++)
		{
			System.out.println(stu[i]);
		}
		Arrays.sort(stu);
		System.out.println("排序之后：");
		for(int i=0;i<stu.length;i++)
		{
			System.out.println(stu[i]);
		}
	}

}
